package com.gmzg.gmzgnews.me.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.gmzg.gmzgnews.R;

/**
 * author: GD on 2016/11/1 20:00
 * email :2301722691@qq.com
 */

public class CircleImageView extends ImageView {

    //view的高度
    private int mWidth;
    //圆形的半径
    private int mRadius;
    //位图着色器
    private BitmapShader mBitmapShader;
    //矩阵--用于缩放图片以适应view控件的大小
    private Matrix mMatrix;
    //图形图像的paint
    private Paint mBitmapPaint;
    //引用绘制图片的外边界
    private Paint mBorderPaint;
    //边界宽度
    private float mStkoredWidth;
    //边界颜色
    private int mStrokeColor;


    public CircleImageView(Context context) {
        super(context);
    }

    /**
     * 初始化
     * @param context
     * @param attrs
     */
    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMatrix = new Matrix();
        mBitmapPaint = new Paint();
        mBitmapPaint.setAntiAlias(true);//反锯齿


        //获取布局文件里自定义控件设置的属性值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView);
        typedArray.recycle();//回收属性对象

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /**
         * 因为我们要绘制图形，所以强制使宽高一致，已小的值为准，以防在布局使用控件时设置height，width不一致的情况
         */
        mWidth = Math.min(getMeasuredHeight(),getMeasuredWidth());
        mRadius = mWidth / 2;
        setMeasuredDimension(mWidth,mWidth);
    }

    /**
     * 设置BitmapShader以及涂料Paint
     */
    private void setBitmapShader(){
        //首先获得drawable对象，也就是控件属性的src,也就是我们的图片
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }
        //将图片转为Bitmap
        BitmapDrawable bd = (BitmapDrawable) drawable;
        Bitmap bitmap = bd.getBitmap();
        //使用bitmap创建一个Bitmapshader，将bitmap载入着色器，后面两个参数为x、y轴的缩放模式，CLAMP为拉伸
        mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
        //初始化图片与view之间的伸缩比例，因为比例一般非整数，用float
        float scale = 1.0f;
        //将图片的宽高的小者作为图片的边长，用来和view计算伸缩比例
        int bitmapSize = Math.min(bitmap.getWidth(),bitmap.getHeight());
        //计算缩放比例，view的大小与图片大小之间的比例
        scale = mWidth *1.0f/bitmapSize;
        //用scale设置变化矩阵的伸缩比例，长宽均以相同比例伸缩
        mMatrix.setScale(scale,scale);
        //给shader设置变换矩阵，绘制时，就会根据view的大小设置图片的大小
        //使用突变的小边缩放到与view大小一致，这样就避免了图片过小导致拉伸或过大导致显示不全
        mBitmapShader.setLocalMatrix(mMatrix);
        //给paint上shader涂料
        mBitmapPaint.setShader(mBitmapShader);
    }

    /**
     * 设置外边界绘制paint
     */
    private void setBorderPaint(){
        //设置笔刷样式，源区域掏空
        mBorderPaint.setStyle(Paint.Style.STROKE);
        //给笔刷上色
        mBorderPaint.setColor(mStrokeColor);
        //边界类型为原型
        mBorderPaint.setStrokeCap(Paint.Cap.ROUND);
        //设置边界的宽度
        mBorderPaint.setStrokeWidth(mStkoredWidth);
        //设置阴影
        this.setLayerType(LAYER_TYPE_SOFTWARE,mBorderPaint);
        mBorderPaint.setShadowLayer(12.0f,3.0f,3.0f, Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);记得去掉父类的ondraw，否则会先绘制ImageView默认的团
        if (getDrawable() == null) {
            return;
        }
        //绘制内部圆形图片
        setBitmapShader();
        canvas.drawCircle(mWidth/2,mWidth/2,mRadius,mBitmapPaint);
    }
}

